﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestGenerator
{
    public class Question
    {
        private string questionText;

        public string QuestionText
        {
            get { return questionText; }
        }

        private HashSet<string> correctAnswers;
        private HashSet<string> wrongAnswers;

        public Question(string question)
        {
            this.questionText = question;
            this.correctAnswers = new HashSet<string>();
            this.wrongAnswers = new HashSet<string>();
        }

        public void AddCorrectAnswer(string answer)
        {
            if (correctAnswers.Contains(answer))
            {
                Console.WriteLine("The question {0} already contains an answer \"{1}\".", this.questionText, answer);
                return;
            }
            this.correctAnswers.Add(answer);
        }

        public void AddWrongAnswer(string answer)
        {
            if (wrongAnswers.Contains(answer))
            {
                Console.WriteLine("The question {0} already contains an answer \"{1}\".", this.questionText, answer);
                return;
            }
            this.wrongAnswers.Add(answer);
        }

        public string GetCorrectAnswer()
        {
            Random random = new Random();
            int index = random.Next(0, correctAnswers.Count);
            string correctAnswer = correctAnswers.ElementAt(index);
            return correctAnswer;
        }

        public HashSet<string> GetWrongAnswers(int max)
        {
            HashSet<string> answers = new HashSet<string>();
            Random random = new Random();
            int availableAnswers = this.wrongAnswers.Count;
            bool[] used = new bool[availableAnswers];
            while (max > 0 && availableAnswers > 0)
            {
                int index = random.Next(0, wrongAnswers.Count);
                if (!used[index])
                {
                    string wrongAnswer = wrongAnswers.ElementAt(index);
                    answers.Add(wrongAnswer);
                    used[index] = true;
                    max--;
                    availableAnswers--;
                }
            }
            return answers;
        }

        public bool HasCorrectAnswer()
        {
            return correctAnswers.Count > 0;
        }

        public bool HasWrongAnswers()
        {
            return wrongAnswers.Count > 0;
        }
    }
}